////////////////////////////////////////////////////////////
// Workfile : Epcos.cpp
// Author : Andreas Lehner
// Date : 11.11.2012
// Description : class Epcos: implementation of Adapter class Epcos
// Remarks : -
// Revision : 1
////////////////////////////////////////////////////////////
#include <string>
#include "Epcos.h"

Epcos::~Epcos(){
	if (mCrypter != 0) {
		delete mCrypter; mCrypter = 0;
	}
}

void Epcos::EncryptRSA(std::string const& fileName){
	if (mCrypter == 0){
		//create a new Crypter
		mCrypter = CrypterFactory::CreateCrypter(CrypterFactory::eRSA);
	}
	//read specified file -> throw exception if reading impossible
	if(!mCrypter->ReadFile(fileName)) throw "ReadFile: Error in reading...";
	//encrypt file
	mCrypter->Encrypt();
	//write to specified file with file extension -> throw exception if writing impossible
	if(!mCrypter->WriteFile(mCrypter->getOutputFilename(fileName))) throw "WriteFile: Error in writing...";
}

void Epcos::DecryptRSA(std::string const& fileName){
	if (mCrypter == 0){
		//create a new Crypter
		mCrypter = CrypterFactory::CreateCrypter(CrypterFactory::eRSA);
	}
	//read specified file -> throw exception if reading impossible
	if(!mCrypter->ReadFile(fileName)) throw "ReadFile: Error in reading...";
	//decrypt file
	mCrypter->Decrypt();
	//write to specified file with file extension -> throw exception if writing impossible
	if(!mCrypter->WriteFile(mCrypter->getRawFilename(fileName))) throw "WriteFile: Error in writing...";
}
